<?php

class X2FlowRecordCreateAction extends X2FlowAction {

    public $title = 'Create Action for Record';
    public $info = 'Creates a new action associated with the record that triggered this flow.';

    public function paramRules(){
        return array(
            'title' => Yii::t('studio', $this->title),
            'info' => Yii::t('studio', $this->info),
            'modelRequired' => 1,
            'options' => array(
                // array('name'=>'attributes'),
                array('name' => 'subject', 'label' => Yii::t('actions', 'Subject'), 'optional' => 1),
                array('name' => 'description', 'label' => Yii::t('actions', 'Description'), 'type' => 'text')
                ));
    }

    public function execute(&$params){
        $action = new Actions;
        $action->associationType = lcfirst(get_class($params['model']));
        $action->associationId = $params['model']->id;
        $action->subject = $this->parseOption('subject', $params);
        $action->actionDescription = $this->parseOption('description', $params);
        if($params['model']->hasAttribute('assignedTo'))
            $action->assignedTo = $params['model']->assignedTo;
        if($params['model']->hasAttribute('priority'))
            $action->priority = $params['model']->priority;
        if($params['model']->hasAttribute('visibility'))
            $action->visibility = $params['model']->visibility;

        if ($action->save()) {
            return array (
                true, 
                Yii::t('studio', "View created action: ").$action->getLink ()
            );
        } else {
            return array (false, $action->getError ());
        }

    }

}

